package cn.seaboot.admin.logger.service;

import cn.seaboot.admin.logger.bean.LoginLog;
import cn.seaboot.admin.logger.dao.LoginLogDao;
import cn.seaboot.commons.core.UUIDUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * Service
 * 登录日志 [t_sys_log_login]
 *
 * @author Mr.css on 2018-6-20
 */
@Service
public class LoginLogService {

    @Resource
    private LoginLogDao loginLogDao;

    /**
     * 新增
     *
     * @param channel 登录通道
     * @param id      用户ID
     * @param account 账号
     * @param token   密钥
     * @param remote  IP
     * @param agent   登录终端信息
     * @return affected rows
     */
    public int insert(String channel, String id, String account, String token, String remote, String agent, Integer status) {
        LoginLog pojo = new LoginLog();
        pojo.setId(UUIDUtils.genUUID32());
        pojo.setUserId(id);

        pojo.setUserName(account);
        pojo.setToken(token);

        pojo.setRemote(remote);
        pojo.setUserAgent(agent);
        pojo.setChannel(channel);

        pojo.setStatus(status);
        pojo.setGmtCreate(LocalDateTime.now());
        return loginLogDao.insert(pojo);
    }

    /**
     * 删
     *
     * @param id id
     * @return affected rows
     */
    public int deleteById(String id) {
        return loginLogDao.deleteById(id);
    }

    /**
     * 改
     *
     * @param pojo pojo
     * @return affected rows
     */
    public int updateById(LoginLog pojo) {
        return loginLogDao.updateSampleById(pojo);
    }

    /**
     * 查
     *
     * @param id id
     * @return pojo
     */
    public LoginLog queryById(String id) {
        return loginLogDao.selectById(id);
    }

    /**
     * 查列表
     *
     * @param params 参数
     * @return list
     */
    public List<LoginLog> queryList(Map<String, Object> params) {
        return loginLogDao.selectList(params);
    }
}
